package com.kingwood.algorithm.test;

import java.util.Scanner;

public class ShuangZhiZhen {

    /**
     * 获取可购买的最多宝石数量
     * @param arr
     * @param v
     * @return
     */
    public static int getMaxBuyCount(int arr[], int v) {
        int maxBuy = 0;
        int l=0, r = 0;
        int window_sum = 0;
        int len = arr.length;

        outer:
        while (r < len) {
            window_sum += arr[r];
            if (window_sum <= v) {
                r++;
            } else {
                maxBuy = Math.max(maxBuy, r - l);

                while (l < r) {
                    window_sum -= arr[l++];
                    if (window_sum <= v) {
                        r++;
                        continue outer;
                    }
                }

                l = ++r;
                window_sum = 0;
            }
        }

        if (window_sum < v) {
            maxBuy = Math.max(maxBuy, r-l);
        }

        return maxBuy;
    }

    public static int getMaxBuyCount2(int arr[], int v) {
        int l = 0, r = 0;
        int windowSum = 0;
        int n = arr.length;
        int ans = 0;

        outer:
        while (r < n) {
            windowSum += arr[r];
            if (windowSum <= v) {
                r++;
            } else {
                ans = Math.max(ans, r - l);
                while (l < r) {
                    windowSum -= arr[l++];
                    if (windowSum <= v) {
                        r++;
                        continue outer;
                    }
                }

                l = ++r;
                windowSum = 0;
            }
        }

        if (windowSum < v) {
            ans = Math.max(ans, r - l);
        }
        return ans;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] arr = new int[n];

        for (int i=0; i<n; i++) {
            arr[i] = scanner.nextInt();
        }

        int v = scanner.nextInt();
        System.out.println(getMaxBuyCount(arr, v));
        System.out.println(getMaxBuyCount2(arr, v));
    }
}
